// Copyright 2007 Google Inc. All Rights Reserved.

package com.google.jessewilson.codesearch.commandlinesearch;

import com.google.jessewilson.codesearch.api.SearchProvider;
import com.google.jessewilson.codesearch.api.spi.SearchProviderParameters;
import com.google.jessewilson.codesearch.io.jdom.EasyElement;
import com.google.jessewilson.codesearch.io.jdom.Externalizer;
import org.jdom.Element;

/**
 * Extra metadata for the {@link ClsSearchProviderBackend}.
 *
 * @author jessewilson
 */
public class ClsSearchProviderParameters implements SearchProviderParameters {

  private final String extraArguments;
  private final boolean convertToRegex;

  public ClsSearchProviderParameters(String extraArguments, boolean convertToRegex) {
    assert(extraArguments != null);
    this.extraArguments = extraArguments;
    this.convertToRegex = convertToRegex;
  }

  public String getExtraArguments() {
    return extraArguments;
  }

  public boolean convertToRegex() {
    return convertToRegex;
  }

  public SearchProvider getSearchProvider() {
    return new ClsSearchProvider();
  }

  public static class MyExternalizer implements Externalizer<ClsSearchProviderParameters> {

    private static final String EXTRA_ARGUMENTS = "extraArguments";
    private static final String CONVERT_TO_REGEX = "convertToRegex";

    public ClsSearchProviderParameters readExternal(Element element) {
      EasyElement easyElement = new EasyElement(element);
      String extraArguments = easyElement.getString(EXTRA_ARGUMENTS, "");
      boolean convertToRegex = easyElement.getBoolean(CONVERT_TO_REGEX, false);
      return new ClsSearchProviderParameters(extraArguments, convertToRegex);
    }

    public void writeExternal(Element element, ClsSearchProviderParameters queryExtra) {
      EasyElement easyElement = new EasyElement(element);
      easyElement.putString(EXTRA_ARGUMENTS, queryExtra.extraArguments);
      easyElement.putBoolean(CONVERT_TO_REGEX, queryExtra.convertToRegex);
    }
  }
}
